void SetHeaderInfo() {
	SendDlgItemMessage(hMainDlg,IDC_HEADER,LB_RESETCONTENT,0,0); //clear header list

	cRawHeader *lprhHeader=&rrRawFile.rbBlock.rhHeader;

	for(int iKeyword=0; iKeyword<lprhHeader->iKeywords-1; iKeyword++) { //populate header list
		sprintf(szMessage,"%-10s %s",
				lprhHeader->lprgkwKeywords[iKeyword].szName,
				lprhHeader->lprgkwKeywords[iKeyword].szValue);
		
		SendDlgItemMessage(hMainDlg,IDC_HEADER,LB_INSERTSTRING,iKeyword,(LPARAM)((LPSTR)szMessage)); 
	}
}

int PointInRect(POINT &pPoint, RECT rRect) {
	if(pPoint.x<rRect.left) return 0;
	if(pPoint.x>rRect.right) return 0;
	if(pPoint.y<rRect.top) return 0;
	if(pPoint.y>rRect.bottom) return 0;
	return 1;
}
				
void DrawHighlight() { //draw highlight around selected sample
	rSample.left=rPlot.left+CHAN_TO_X(iSelChan);
	rSample.right=rSample.left+iChanWidth-1;
	rSample.top=rPlot.top+TIME_TO_Y(ulSelTime);
	rSample.bottom=rPlot.top+TIME_TO_Y(ulSelTime+ulSelRange);
	
	//bound to plot view
	if(rSample.top<rPlot.top) rSample.top=rPlot.top;
	else if(rSample.top>rPlot.bottom) rSample.top=rPlot.bottom;
	if(rSample.bottom<rPlot.top) rSample.bottom=rPlot.top;
	else if(rSample.bottom>rPlot.bottom) rSample.bottom=rPlot.bottom;
	
	SelectObject(hdcWnd,hpGreenPen);
	SelectObject(hdcWnd,hbNullBrush);
	Rectangle(hdcWnd,rSample.left,rSample.top,rSample.right,rSample.bottom);
}

void SetSelByPoints() {
	//click not within plot bounds
	if(!PointInRect(pClick,rPlot)) return;
	if(!PointInRect(pDrag,rPlot))  return;
	
	iSelChan=X_TO_CHAN(pClick.x-rPlot.left);
	ulSelTime=Y_TO_TIME(pClick.y-rPlot.top);
	ulSelRange=Y_TO_TIME(pDrag.y-rPlot.top);

	if(ulSelRange<ulSelTime) {
		unsigned long ulTemp=ulSelTime;
		ulSelTime=ulSelRange;
		ulSelRange=ulTemp;	
	}

	ulSelRange-=ulSelTime;
	ulSelRange=MAX(ulSelRange,(unsigned int)iTimeZoom);
}

void SetSelSample() {	
	if(iSelChan>rrRawFile.rbBlock.iObsChans) return;
	if(ulSelTime>rrRawFile.rbBlock.ulObsTimes) return;
	
	SendMessage(hMainDlg,WM_PAINT,0,0);

	//get sample info
	if(!rrRawFile.rbBlock.GetSample(iSelChan,ulSelTime,ulSelRange,rsSample)) {
		rsSample.GetInfo(szMessage);
		SetDlgItemText(hMainDlg,IDC_SAMPLE_EDIT,szMessage);
	}
}						

void DrawAxisBk() { //background for time/freq labels
	SetBkColor(hdcWnd,RGB(255,255,255));
	SelectObject(hdcWnd,hpNullPen);
	SelectObject(hdcWnd,hbWhiteBrush);

	Rectangle(hdcWnd,rPlot.left-60,rPlot.top-2,rPlot.left-2,rPlot.top+iPlotHeight+5); //left
	Rectangle(hdcWnd,rPlot.left-60,rPlot.top+iPlotHeight+2,rScroll.right-2,rPlot.top+iPlotHeight+32); //bottom
	Rectangle(hdcWnd,rPlot.left-60,rPlot.top-11,rScroll.right-2,rPlot.top-1); //top
}

void DrawTimeAxis() { //time axis labels
	int iTimeSect=11; //number of labels
	int iTimeInc=((iPlotHeight/iTimeSect)*iTimeZoom)/iTimeHeight;

	//draw times
	for(int ulTime=ulTimeStart; TIME_IN_PLOT(TIME_TO_Y(ulTime)); ulTime+=iTimeInc) {
		GetTimeString(ulTime*rrRawFile.rbBlock.dSampLen,szMessage);
		int iTextWidth=(strlen(szMessage)*(iCharWidth));
		int iXPos=rPlot.left-iTextWidth-2;
		int iYPos=rPlot.top+TIME_TO_Y(ulTime)-2;
		TextOut(hdcWnd,iXPos,iYPos,szMessage,strlen(szMessage));
	}
}

void DrawFreqAxis() {
	//draw freqs
	int iLastEnd=0;
	int iXPos=rPlot.left-40;
	int iYPos=rPlot.top+iPlotHeight+10;
	sprintf(szMessage,"MHz");
	TextOut(hdcWnd,iXPos,iYPos,szMessage,strlen(szMessage));

	for(int iChan=0; iChan<rrRawFile.rbBlock.iObsChans; iChan++) {
		//for(int iSide=0; iSide<2; iSide++) {
			
			iXPos=rPlot.left+CHAN_TO_X(iChan)/*+SIDE_TO_X(iSide)*/;
			if(iXPos<iLastEnd) continue; //skip if it would draw in last label

			double dFreq=rrRawFile.rbBlock.lprgdChanFreq[iChan];
			//double dShift=rrRawFile.rbBlock.dChanBW/2;
			//dFreq+=(iSide? dShift:-dShift);
			sprintf(szMessage,"%.2f",dFreq);
			int iTextWidth=(strlen(szMessage)*(iCharWidth));
			if((iXPos+iTextWidth)>=rScroll.right) break;

			iLastEnd=iXPos+(strlen(szMessage)*(iCharWidth));
				
			TextOut(hdcWnd,iXPos,iYPos,szMessage,strlen(szMessage));
		//}
	}
}

void DrawPlot() { 
	//plot outline
	SelectObject(hdcWnd,hpBlackPen);
	SelectObject(hdcWnd,hbNullBrush);
	Rectangle(hdcWnd,rPlot.left-1,rPlot.top-1,rPlot.right+1,rPlot.bottom+2);

	//copy plot from its device context
	if(!BitBlt(hdcWnd,rPlot.left,rPlot.top,iPlotWidth,iPlotHeight,hdcMem,0,0,SRCCOPY)) 
		int iError=GetLastError();

	DrawHighlight(); //highlight selected sample
	DrawAxisBk(); //draw axis background

	if(rrRawFile.iBlocks) { //only if file loaded
		DrawTimeAxis();
		DrawFreqAxis();
	}
}

void RenderPlot() { //render plot to memory device context
	unsigned long ulTime;

	SelectObject(hdcMem,hbBlackBrush);
	Rectangle(hdcMem,0,0,iPlotWidth,iPlotHeight);

	for(int iChan=0; iChan<rrRawFile.rbBlock.iObsChans; iChan++) {
		for(ulTime=ulTimeStart; TIME_IN_PLOT(TIME_TO_Y(ulTime)); ulTime+=iTimeZoom) {			
			if(ulTime>=rrRawFile.rbBlock.ulObsTimes) break;

			rSample.top=TIME_TO_Y(ulTime);
			rSample.bottom=rSample.top+iTimeHeight+1;

			rrRawFile.rbBlock.GetSample(iChan,ulTime,iTimeZoom,rsSample);

			for(int iSide=0; iSide<2; iSide++) {
				rSample.left=CHAN_TO_X(iChan)+SIDE_TO_X(iSide);
				rSample.right=rSample.left+SIDE_TO_X(1);
				
				HBRUSH hbTemp=CreateSolidBrush(RGB(rsSample.iR[iSide],rsSample.iG[iSide],rsSample.iB[iSide]));
				SelectObject(hdcMem,hbTemp);
				Rectangle(hdcMem,rSample.left,rSample.top,rSample.right,rSample.bottom);
				DeleteObject((HANDLE)hbTemp);
			}
		}
	}

	DrawPlot();
}

void SetTimeOffset() {
	sprintf(szMessage,"%u",ulTimeStart);
	SetDlgItemText(hMainDlg,IDC_SAMPOFF_EDIT,szMessage);

	sprintf(szMessage,"%u",rrRawFile.rbBlock.ulObsTimes);
	SetDlgItemText(hMainDlg,IDC_SAMPMAX_EDIT,szMessage);

	GetTimeString(ulTimeStart*rrRawFile.rbBlock.dSampLen,szMessage);
	SetDlgItemText(hMainDlg,IDC_TIMEOFF_EDIT,szMessage);

	GetTimeString(rrRawFile.rbBlock.dBlockLen,szMessage);
	SetDlgItemText(hMainDlg,IDC_TIMEMAX_EDIT,szMessage);
}

void SetScrollBar() {
	ulMaxTimeStart=(rrRawFile.rbBlock.ulObsTimes)-Y_TO_SAMP(iPlotHeight);
	ulTimeStart=MIN(ulTimeStart,ulMaxTimeStart);
	SetScrollRange(hTimeScroll,SB_CTL,0,ulMaxTimeStart,FALSE);
	SetScrollPos(hTimeScroll,SB_CTL,ulTimeStart,TRUE);
}

void SetPlotInfo() {
	iChanWidth=iPlotWidth/rrRawFile.rbBlock.iObsChans;
	iMaxZoom=int(rrRawFile.rbBlock.ulObsTimes/iPlotHeight);
	
	SetScrollBar();
	SetTimeOffset();

	//height & zoom
	SendDlgItemMessage(hMainDlg,IDC_HEIGHT_SPIN,UDM_SETRANGE,1,8);
	SendDlgItemMessage(hMainDlg,IDC_ZOOM_SPIN,UDM_SETRANGE,1,(rrRawFile.rbBlock.ulObsTimes/iPlotHeight));

	RenderPlot();
}

void SetDlgInfo() {
	ulSelRange=MAX(ulSelRange,(unsigned int)iTimeZoom);
	SetSelSample();
	SetHeaderInfo();
	SetPlotInfo();
}
